package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apicultor;

import com.model.Apiario;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table APIARIO.
*/
public class ApiarioDao extends AbstractDao<Apiario, Long> {

    public static final String TABLENAME = "APIARIO";

    /**
     * Properties of entity Apiario.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_idApiario");
        public final static Property NumApiario = new Property(1, int.class, "numApiario", false, "NUM_APIARIO");
        public final static Property Identificacao = new Property(2, String.class, "Identificacao", false, "IDENTIFICACAO");
        public final static Property DataInstalacao = new Property(3, String.class, "DataInstalacao", false, "DATA_INSTALACAO");
        public final static Property Localizacao = new Property(4, String.class, "localizacao", false, "LOCALIZACAO");
        public final static Property Obs = new Property(5, String.class, "obs", false, "OBS");
        public final static Property NColonias = new Property(6, int.class, "nColonias", false, "N_COLONIAS");
        public final static Property Altura = new Property(7, String.class, "altura", false, "ALTURA");
        public final static Property Largura = new Property(8, String.class, "largura", false, "LARGURA");
        public final static Property Latitude = new Property(9, String.class, "latitude", false, "LATITUDE");
        public final static Property Longitude = new Property(10, String.class, "longitude", false, "LONGITUDE");
        public final static Property Orientacao = new Property(11, String.class, "orientacao", false, "ORIENTACAO");
        public final static Property ProxiAgua = new Property(12, Integer.class, "proxiAgua", false, "PROXI_AGUA");
        public final static Property Local = new Property(13, String.class, "local", false, "LOCAL");
        public final static Property ZonaControlada = new Property(14, Boolean.class, "zonaControlada", false, "ZONA_CONTROLADA");
        public final static Property CultIntensa = new Property(15, Boolean.class, "cultIntensa", false, "CULT_INTENSA");
        public final static Property Transumanico = new Property(16, Boolean.class, "transumanico", false, "TRANSUMANICO");
        public final static Property Lab = new Property(17, Boolean.class, "lab", false, "LAB");
        public final static Property Desactivar = new Property(18, Boolean.class, "desactivar", false, "DESACTIVAR");
        public final static Property Distrito = new Property(19, String.class, "distrito", false, "DISTRITO");
        public final static Property Concelho = new Property(20, String.class, "Concelho", false, "CONCELHO");
        public final static Property Freguesia = new Property(21, String.class, "freguesia", false, "FREGUESIA");
        public final static Property Codpostal = new Property(22, String.class, "codpostal", false, "CODPOSTAL");
        public final static Property Codigoresidencia = new Property(23, String.class, "codigoresidencia", false, "CODIGORESIDENCIA");
        public final static Property Foto = new Property(24, String.class, "foto", false, "FOTO");
        public final static Property Zonacontrol = new Property(25, String.class, "zonacontrol", false, "ZONACONTROL");
        public final static Property ProdMel = new Property(26, Integer.class, "prodMel", false, "PROD_MEL");
        public final static Property ProdPropolis = new Property(27, Integer.class, "prodPropolis", false, "PROD_PROPOLIS");
        public final static Property ProdPolen = new Property(28, Integer.class, "prodPolen", false, "PROD_POLEN");
        public final static Property CodApicultor = new Property(29, long.class, "codApicultor", false, "COD_APICULTOR");
    };

    private DaoSession daoSession;

    private Query<Apiario> apicultor_ApiarioApicultorQuery;

    public ApiarioDao(DaoConfig config) {
        super(config);
    }
    
    public ApiarioDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'APIARIO' (" + //
                "'_idApiario' INTEGER PRIMARY KEY ASC AUTOINCREMENT ," + // 0: id
                "'NUM_APIARIO' INTEGER NOT NULL ," + // 1: numApiario
                "'IDENTIFICACAO' TEXT NOT NULL ," + // 2: Identificacao
                "'DATA_INSTALACAO' TEXT NOT NULL ," + // 3: DataInstalacao
                "'LOCALIZACAO' TEXT," + // 4: localizacao
                "'OBS' TEXT," + // 5: obs
                "'N_COLONIAS' INTEGER NOT NULL ," + // 6: nColonias
                "'ALTURA' TEXT," + // 7: altura
                "'LARGURA' TEXT," + // 8: largura
                "'LATITUDE' TEXT," + // 9: latitude
                "'LONGITUDE' TEXT," + // 10: longitude
                "'ORIENTACAO' TEXT," + // 11: orientacao
                "'PROXI_AGUA' INTEGER," + // 12: proxiAgua
                "'LOCAL' TEXT," + // 13: local
                "'ZONA_CONTROLADA' INTEGER," + // 14: zonaControlada
                "'CULT_INTENSA' INTEGER," + // 15: cultIntensa
                "'TRANSUMANICO' INTEGER," + // 16: transumanico
                "'LAB' INTEGER," + // 17: lab
                "'DESACTIVAR' INTEGER," + // 18: desactivar
                "'DISTRITO' TEXT," + // 19: distrito
                "'CONCELHO' TEXT," + // 20: Concelho
                "'FREGUESIA' TEXT," + // 21: freguesia
                "'CODPOSTAL' TEXT," + // 22: codpostal
                "'CODIGORESIDENCIA' TEXT," + // 23: codigoresidencia
                "'FOTO' TEXT," + // 24: foto
                "'ZONACONTROL' TEXT," + // 25: zonacontrol
                "'PROD_MEL' INTEGER," + // 26: prodMel
                "'PROD_PROPOLIS' INTEGER," + // 27: prodPropolis
                "'PROD_POLEN' INTEGER," + // 28: prodPolen
                "'COD_APICULTOR' INTEGER NOT NULL );"); // 29: codApicultor
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'APIARIO'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Apiario entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getNumApiario());
        stmt.bindString(3, entity.getIdentificacao());
        stmt.bindString(4, entity.getDataInstalacao());
 
        String localizacao = entity.getLocalizacao();
        if (localizacao != null) {
            stmt.bindString(5, localizacao);
        }
 
        String obs = entity.getObs();
        if (obs != null) {
            stmt.bindString(6, obs);
        }
        stmt.bindLong(7, entity.getNColonias());
 
        String altura = entity.getAltura();
        if (altura != null) {
            stmt.bindString(8, altura);
        }
 
        String largura = entity.getLargura();
        if (largura != null) {
            stmt.bindString(9, largura);
        }
 
        String latitude = entity.getLatitude();
        if (latitude != null) {
            stmt.bindString(10, latitude);
        }
 
        String longitude = entity.getLongitude();
        if (longitude != null) {
            stmt.bindString(11, longitude);
        }
 
        String orientacao = entity.getOrientacao();
        if (orientacao != null) {
            stmt.bindString(12, orientacao);
        }
 
        Integer proxiAgua = entity.getProxiAgua();
        if (proxiAgua != null) {
            stmt.bindLong(13, proxiAgua);
        }
 
        String local = entity.getLocal();
        if (local != null) {
            stmt.bindString(14, local);
        }
 
        Boolean zonaControlada = entity.getZonaControlada();
        if (zonaControlada != null) {
            stmt.bindLong(15, zonaControlada ? 1l: 0l);
        }
 
        Boolean cultIntensa = entity.getCultIntensa();
        if (cultIntensa != null) {
            stmt.bindLong(16, cultIntensa ? 1l: 0l);
        }
 
        Boolean transumanico = entity.getTransumanico();
        if (transumanico != null) {
            stmt.bindLong(17, transumanico ? 1l: 0l);
        }
 
        Boolean lab = entity.getLab();
        if (lab != null) {
            stmt.bindLong(18, lab ? 1l: 0l);
        }
 
        Boolean desactivar = entity.getDesactivar();
        if (desactivar != null) {
            stmt.bindLong(19, desactivar ? 1l: 0l);
        }
 
        String distrito = entity.getDistrito();
        if (distrito != null) {
            stmt.bindString(20, distrito);
        }
 
        String Concelho = entity.getConcelho();
        if (Concelho != null) {
            stmt.bindString(21, Concelho);
        }
 
        String freguesia = entity.getFreguesia();
        if (freguesia != null) {
            stmt.bindString(22, freguesia);
        }
 
        String codpostal = entity.getCodpostal();
        if (codpostal != null) {
            stmt.bindString(23, codpostal);
        }
 
        String codigoresidencia = entity.getCodigoresidencia();
        if (codigoresidencia != null) {
            stmt.bindString(24, codigoresidencia);
        }
 
        String foto = entity.getFoto();
        if (foto != null) {
            stmt.bindString(25, foto);
        }
 
        String zonacontrol = entity.getZonacontrol();
        if (zonacontrol != null) {
            stmt.bindString(26, zonacontrol);
        }
 
        Integer prodMel = entity.getProdMel();
        if (prodMel != null) {
            stmt.bindLong(27, prodMel);
        }
 
        Integer prodPropolis = entity.getProdPropolis();
        if (prodPropolis != null) {
            stmt.bindLong(28, prodPropolis);
        }
 
        Integer prodPolen = entity.getProdPolen();
        if (prodPolen != null) {
            stmt.bindLong(29, prodPolen);
        }
        stmt.bindLong(30, entity.getCodApicultor());
    }

    @Override
    protected void attachEntity(Apiario entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Apiario readEntity(Cursor cursor, int offset) {
        Apiario entity = new Apiario( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getInt(offset + 1), // numApiario
            cursor.getString(offset + 2), // Identificacao
            cursor.getString(offset + 3), // DataInstalacao
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // localizacao
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // obs
            cursor.getInt(offset + 6), // nColonias
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // altura
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // largura
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // latitude
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // longitude
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // orientacao
            cursor.isNull(offset + 12) ? null : cursor.getInt(offset + 12), // proxiAgua
            cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13), // local
            cursor.isNull(offset + 14) ? null : cursor.getShort(offset + 14) != 0, // zonaControlada
            cursor.isNull(offset + 15) ? null : cursor.getShort(offset + 15) != 0, // cultIntensa
            cursor.isNull(offset + 16) ? null : cursor.getShort(offset + 16) != 0, // transumanico
            cursor.isNull(offset + 17) ? null : cursor.getShort(offset + 17) != 0, // lab
            cursor.isNull(offset + 18) ? null : cursor.getShort(offset + 18) != 0, // desactivar
            cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19), // distrito
            cursor.isNull(offset + 20) ? null : cursor.getString(offset + 20), // Concelho
            cursor.isNull(offset + 21) ? null : cursor.getString(offset + 21), // freguesia
            cursor.isNull(offset + 22) ? null : cursor.getString(offset + 22), // codpostal
            cursor.isNull(offset + 23) ? null : cursor.getString(offset + 23), // codigoresidencia
            cursor.isNull(offset + 24) ? null : cursor.getString(offset + 24), // foto
            cursor.isNull(offset + 25) ? null : cursor.getString(offset + 25), // zonacontrol
            cursor.isNull(offset + 26) ? null : cursor.getInt(offset + 26), // prodMel
            cursor.isNull(offset + 27) ? null : cursor.getInt(offset + 27), // prodPropolis
            cursor.isNull(offset + 28) ? null : cursor.getInt(offset + 28), // prodPolen
            cursor.getLong(offset + 29) // codApicultor
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Apiario entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setNumApiario(cursor.getInt(offset + 1));
        entity.setIdentificacao(cursor.getString(offset + 2));
        entity.setDataInstalacao(cursor.getString(offset + 3));
        entity.setLocalizacao(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setObs(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setNColonias(cursor.getInt(offset + 6));
        entity.setAltura(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setLargura(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setLatitude(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setLongitude(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setOrientacao(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setProxiAgua(cursor.isNull(offset + 12) ? null : cursor.getInt(offset + 12));
        entity.setLocal(cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13));
        entity.setZonaControlada(cursor.isNull(offset + 14) ? null : cursor.getShort(offset + 14) != 0);
        entity.setCultIntensa(cursor.isNull(offset + 15) ? null : cursor.getShort(offset + 15) != 0);
        entity.setTransumanico(cursor.isNull(offset + 16) ? null : cursor.getShort(offset + 16) != 0);
        entity.setLab(cursor.isNull(offset + 17) ? null : cursor.getShort(offset + 17) != 0);
        entity.setDesactivar(cursor.isNull(offset + 18) ? null : cursor.getShort(offset + 18) != 0);
        entity.setDistrito(cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19));
        entity.setConcelho(cursor.isNull(offset + 20) ? null : cursor.getString(offset + 20));
        entity.setFreguesia(cursor.isNull(offset + 21) ? null : cursor.getString(offset + 21));
        entity.setCodpostal(cursor.isNull(offset + 22) ? null : cursor.getString(offset + 22));
        entity.setCodigoresidencia(cursor.isNull(offset + 23) ? null : cursor.getString(offset + 23));
        entity.setFoto(cursor.isNull(offset + 24) ? null : cursor.getString(offset + 24));
        entity.setZonacontrol(cursor.isNull(offset + 25) ? null : cursor.getString(offset + 25));
        entity.setProdMel(cursor.isNull(offset + 26) ? null : cursor.getInt(offset + 26));
        entity.setProdPropolis(cursor.isNull(offset + 27) ? null : cursor.getInt(offset + 27));
        entity.setProdPolen(cursor.isNull(offset + 28) ? null : cursor.getInt(offset + 28));
        entity.setCodApicultor(cursor.getLong(offset + 29));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Apiario entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Apiario entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "apiarioApicultor" to-many relationship of Apicultor. */
    public List<Apiario> _queryApicultor_ApiarioApicultor(long codApicultor) {
        synchronized (this) {
            if (apicultor_ApiarioApicultorQuery == null) {
                QueryBuilder<Apiario> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodApicultor.eq(null));
                apicultor_ApiarioApicultorQuery = queryBuilder.build();
            }
        }
        Query<Apiario> query = apicultor_ApiarioApicultorQuery.forCurrentThread();
        query.setParameter(0, codApicultor);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApicultorDao().getAllColumns());
            builder.append(" FROM APIARIO T");
            builder.append(" LEFT JOIN APICULTOR T0 ON T.'COD_APICULTOR'=T0.'_idApicultor'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Apiario loadCurrentDeep(Cursor cursor, boolean lock) {
        Apiario entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }

        return entity;    
    }

    public Apiario loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Apiario> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Apiario> list = new ArrayList<Apiario>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Apiario> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Apiario> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
